package com.hns.can.energy.web;

import com.hns.can.common.domain.GridPage;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.component.ParseElePaperExcel;
import com.hns.can.energy.entity.EaiIttriplogEntity;
import com.hns.can.energy.service.EleDetailService;
import com.hns.can.energy.vo.ParamVo;
import com.hns.can.master.component.ParseStnLgtLttExcel;
import com.hns.can.master.entity.BaseStnInfoEntity;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.export.excel.excelNoTemplate.ExportExcelNoTempEntity;
import com.hns.tool.export.pdf.ExportPdfEntity;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.FileUploadUtil;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.ResponseUtil;
import com.hns.tool.pub.UtilString;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * 电子更纸明Controller
 * @author Chenl
 *
 */
@Controller
@RequestMapping("/eleDetail")
public class EleDetailController extends BaseController{
    @Autowired
    EleDetailService eleDetailService;

    final private String[] headerNames = new String[]{"营运日期","车牌号","车辆编号","司机姓名","发出站编码","终点站编码","发出站","终点站","任务",
            "发出时间","到对站时间","生成时间","时间戳","运行时间","里程","空程","备注","方向"};
    final private String[] headerFields = new String[]{"rundate","licPltNo", "carInCd" ,"drivename","startstopcode","destinationcode",
            "startstopname","destinationname", "task", "dispatchstarttime","endtriptime", "departinterval",
            "anchortime","runtime", "runmileage", "mileageoutofservice","remark", "rundirection"};
    /**
     * 查询
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping("/searchEleDetail")
    @ResponseBody
    public Object searchEleDetail(ParamVo vo) throws Exception {
        Pagination page = eleDetailService.searchEleDetail(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 电子更纸明细导出excel
     * @param vo
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportEleDetailExcel")
    public void exportEleDetailExcel(ParamVo vo, HttpServletResponse response) throws Exception {
        List list = eleDetailService.exportEleDetail(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportExcelNoTempEntity exportEntity = new ExportExcelNoTempEntity(
                Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("电子更纸明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        ExportUtil.generatorExcelNoTemplate(exportEntity, response);
    }

    /**
     * 电子更纸明细导出PDF
     * @param vo
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportEleDetailPdf")
    public void exportEleDetailPdf(ParamVo vo, HttpServletResponse response) throws Exception {
        List list = eleDetailService.exportEleDetail(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportPdfEntity pdfEntity = new ExportPdfEntity(
                Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("电子更纸明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        ExportUtil.generatorPDF(pdfEntity, response);
    }
    
    /**
     * 上传电子更纸信息
     * @param elePaper
     * @param request
     * @return
     * @throws Exception
     * @author:Chenl
     * @email:370238764@qq.com
     */
    @RequestMapping(value = "/uploadElePaperFile", method = RequestMethod.POST)
    @ResponseBody
    public Object uploadElePaperFile(
        @RequestParam MultipartFile elePaperFile,
                HttpServletRequest request,String corpId,String orgId,HttpServletResponse response) throws Exception {
    	// 将excel文件存盘
        String oldFileName = elePaperFile.getOriginalFilename();
        String newFileName = UtilString.getUUID() + oldFileName.substring(oldFileName.lastIndexOf("."));
        String targetPath = FileUploadUtil.getWebRoot(request) + "/template/temp/";
        FileUploadUtil.uploadFile(elePaperFile.getInputStream(), newFileName, targetPath);
        // 解析excel文件得到list对象
        String fileName = targetPath + newFileName; // 完整路径
        File file = new File(fileName);
        int count = 0;
      //设置响应给前台内容的数据格式
//        response.setContentType("text/plain; charset=UTF-8");
        //检查是否是珍宝
        if("8650bf67257a49fc9bdcebec6555ac44".equals(corpId)){
        	List<EaiIttriplogEntity> list = ParseElePaperExcel.parseZhenBaoExcelToList(file);
        	if(PubUtil.isEmpty(list)){
        		ResponseUtil.responseJSON("请导入正确模板", response);
                return null;
        	}
        	//// 存入对应的库表
        	count = eleDetailService.saveZhenBaoToDB(list, this.getCurrentUser().getUser(),corpId,orgId);
        	//检查是否是晋江
        }else if("24243e69b1fe46c9af843470822c74fa".equals(corpId)){
        	List<EaiIttriplogEntity> list = ParseElePaperExcel.parseJinJiangExcelToList(file);
        	if(PubUtil.isEmpty(list)){
        		ResponseUtil.responseJSON("请导入正确模板", response);
                return null;
        	}
        	//// 存入对应的库表
        	count = eleDetailService.saveJinJiangToDB(list, this.getCurrentUser().getUser(),corpId,orgId);
        	
        }
        ResponseUtil.responseJSON("导入成功,共导入数据"+count+"行。", response);
        return null;
//        return this.getReturnMsg("导入成功,共导入数据"+count+"行。");
    }
    
    /**
     * 触发线路偏移的纠正算法
     * @param String corpId
     * @param request
     * @return
     * @throws Exception
     * @author:Chenl
     * @email:370238764@qq.com
     */
    @RequestMapping("/triggerAlgorithm")
    @ResponseBody
    public void triggerAlgorithm(String corpId) throws Exception {
    	eleDetailService.triggerAlgorithm(corpId);
    }
    

}
